﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Power.Models;
using Power.View;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Markup;

namespace Power.ViewModel
{
    public class EvaluationRecordViewModel: ViewModelBase
    {
        public EvaluationRecord model;
        public EvaluationRecordViewModel(EvaluationRecord model)
        {
            this.model = model;
            this.model.page.PageUpdated += Page_PageUpdated;
            OpenSelect = new RelayCommand(OnOpenSelect);

            ColumnCheckBox = new RelayCommand<ColumnListItem>(OnColumnCheckBox);
            Data = new EvaluationRecordEntity()
            {
                EvaluateRecordEntityList = new ObservableCollection<EvaluateRecordEntity>()
                {
                    new EvaluateRecordEntity()
                {
                    ID = 1,
                    Area = "区域",
                    Block = "区块",
                    WorkField= "工点",
                    StartWell = new WellEntity()
                    {
                        No = "2",
                        Type = "1",
                        FuncType = "1",
                        Mat = "1",
                        Problem = "1",

                    },
                    EndWell = new WellEntity()
                    {

                        No = "2",
                        Type = "1",
                        FuncType = "1",
                        Mat = "1",
                        Problem = "1",
                    },
                    Evaluate = new EvaluateEntity()
                    {
                        Sparam = "1",
                        Smax = "1",
                        DefectParam = "1",
                        Flevel = "1",
                        StructDefectDens = "1",
                        SmType = "1",
                        RepairIndex = "1",
                        RepairLevel = "1",
                        RepairSuggest = "1",
                        Yparam = "1",
                        Ymax = "1",
                        YdefectParam = "1",
                        Glevel = "1",
                        FuncDefectDens = "1",
                        YmType = "1",
                        MaintainIndex = "1",
                        MaintainLevel = "1",
                        MaintainSuggest = "1",
                    },
                    Detect = new DetectEntity() {
                        Length = 1,
                        Method = "1",
                        Direction = "1",
                        Date = "1",
                        Batch = 1,
                        Detector = "1",
                        Subcontractors = "1",
                        PicName = "1",
                        Describe = "1",
                        WorkGroup = "1",
                        CollectDetector = "1"
                    },
                    Pip = new PipEntity()
                    {
                        No = 1,
                        BeginBuriedDepth = 1,
                        EndBuriedDepth =9,
                        Type = "1",
                        Mat = "1",
                        Diameter = 1,
                        Length = 1,
                        AreaParam = "1",
                        SolidParam = "1",
                        BuildDate = "1",


                    }
                },
                   new EvaluateRecordEntity()
                {
                    ID = 1,
                    Area = "区域",
                    Block = "区块",
                    WorkField= "工点",
                    StartWell = new WellEntity()
                    {
                        No = "2",
                        Type = "1",
                        FuncType = "1",
                        Mat = "1",
                        Problem = "1",

                    },
                    EndWell = new WellEntity()
                    {

                        No = "2",
                        Type = "1",
                        FuncType = "1",
                        Mat = "1",
                        Problem = "1",
                    },
                    Evaluate = new EvaluateEntity()
                    {
                        Sparam = "1",
                        Smax = "1",
                        DefectParam = "1",
                        Flevel = "1",
                        StructDefectDens = "1",
                        SmType = "1",
                        RepairIndex = "1",
                        RepairLevel = "1",
                        RepairSuggest = "1",
                        Yparam = "1",
                        Ymax = "1",
                        YdefectParam = "1",
                        Glevel = "1",
                        FuncDefectDens = "1",
                        YmType = "1",
                        MaintainIndex = "1",
                        MaintainLevel = "1",
                        MaintainSuggest = "1",
                    },
                    Detect = new DetectEntity() {
                        Length = 1,
                        Method = "1",
                        Direction = "1",
                        Date = "1",
                        Batch = 1,
                        Detector = "1",
                        Subcontractors = "1",
                        PicName = "1",
                        Describe = "1",
                        WorkGroup = "1",
                        CollectDetector = "1"
                    },
                    Pip = new PipEntity()
                    {
                        No = 1,
                        BeginBuriedDepth = 1,
                        EndBuriedDepth =9,
                        Type = "1",
                        Mat = "1",
                        Diameter = 1,
                        Length = 1,
                        AreaParam = "1",
                        SolidParam = "1",
                        BuildDate = "1",


                    }
                   }
                },
                AreaSelectList = Common.Common.GetAreaSelectList(),
                BlockSelectList = Common.Common.GetBlockSelectList(),
                WorkSelectList = Common.Common.GetWorkSelectList(),
                BatchSelectList = Common.Common.GetBatchSelectList(),
                ColumnList = new ObservableCollection<ColumnListItem>() {
                    new ColumnListItem(){IsChecked=true,ColumnName="全部"},
                    new ColumnListItem(){IsChecked=true,ColumnName="序号"},
                    new ColumnListItem(){IsChecked=true,ColumnName="片区"},
                    new ColumnListItem(){IsChecked=true,ColumnName="区块"},
                    new ColumnListItem(){IsChecked=true,ColumnName="工点名称"},
                    new ColumnListItem(){IsChecked=true,ColumnName="起始井号"},
                    new ColumnListItem(){IsChecked=true,ColumnName="起始井类型"},
                    new ColumnListItem(){IsChecked=true,ColumnName="起始井功能类型"},
                    new ColumnListItem(){IsChecked=true,ColumnName="起始井材质"},
                    new ColumnListItem(){IsChecked=true,ColumnName="起始井内外部检查"},
                    new ColumnListItem(){IsChecked=true,ColumnName="是否在道路上"},

                    new ColumnListItem(){IsChecked=true,ColumnName="终止井号"},
                    new ColumnListItem(){IsChecked=true,ColumnName="终止井类型"},
                    new ColumnListItem(){IsChecked=true,ColumnName="终止井功能类型"},
                    new ColumnListItem(){IsChecked=true,ColumnName="终止井材质"},
                    new ColumnListItem(){IsChecked=true,ColumnName="终止井内外部检查"},
                    new ColumnListItem(){IsChecked=true,ColumnName="是否在道路上"},

                    new ColumnListItem(){IsChecked=true,ColumnName="地区参数"},
                    new ColumnListItem(){IsChecked=true,ColumnName="土质参数"},
                    new ColumnListItem(){IsChecked=true,ColumnName="敷设年代"},
                    new ColumnListItem(){IsChecked=true,ColumnName="管段类型"},
                    new ColumnListItem(){IsChecked=true,ColumnName="管段材质"},
                    new ColumnListItem(){IsChecked=true,ColumnName="管段直径"},
                    new ColumnListItem(){IsChecked=true,ColumnName="管段长度"},
                    new ColumnListItem(){IsChecked=true,ColumnName="检测长度"},
                    new ColumnListItem(){IsChecked=true,ColumnName="起点埋深"},
                    new ColumnListItem(){IsChecked=true,ColumnName="终点埋深"},
                    new ColumnListItem(){IsChecked=true,ColumnName="检测方法"},
                    new ColumnListItem(){IsChecked=true,ColumnName="检测方向"},

                    new ColumnListItem(){IsChecked=true,ColumnName="S"},
                    new ColumnListItem(){IsChecked=true,ColumnName="Smax"},
                    new ColumnListItem(){IsChecked=true,ColumnName="缺陷参数"},
                    new ColumnListItem(){IsChecked=true,ColumnName="F等级"},
                    new ColumnListItem(){IsChecked=true,ColumnName="缺陷密度"},
                    new ColumnListItem(){IsChecked=true,ColumnName="SM类型"},
                     new ColumnListItem(){IsChecked=true,ColumnName="修复指数"},
                    new ColumnListItem(){IsChecked=true,ColumnName="修复等级"},
                    new ColumnListItem(){IsChecked=true,ColumnName="修复建议"},

                    new ColumnListItem(){IsChecked=true,ColumnName="Y"},
                    new ColumnListItem(){IsChecked=true,ColumnName="Ymax"},
                    new ColumnListItem(){IsChecked=true,ColumnName="缺陷参数"},
                    new ColumnListItem(){IsChecked=true,ColumnName="G等级"},
                    new ColumnListItem(){IsChecked=true,ColumnName="缺陷密度"},
                    new ColumnListItem(){IsChecked=true,ColumnName="YM类型"},
                     new ColumnListItem(){IsChecked=true,ColumnName="养护指数"},
                    new ColumnListItem(){IsChecked=true,ColumnName="养护等级"},
                    new ColumnListItem(){IsChecked=true,ColumnName="养护建议"},
                },
            };
            
        }

        private void Page_PageUpdated(object sender, HandyControl.Data.FunctionEventArgs<int> e)
        {
            var currentPage = e.Info;
        }

        private void OnColumnCheckBox(ColumnListItem curitem)
        {
            int index = Data.ColumnList.IndexOf(curitem);
            if (index == 0)
            {
                foreach (var item in Data.ColumnList)
                {
                    item.IsChecked = curitem.IsChecked;
                }
            }
            else {
                var first = Data.ColumnList.FirstOrDefault();
                var list = Data.ColumnList.Where(a => a != first).ToList();
                if (list.Where(a => a.IsChecked).Count() == list.Count)
                {
                    first.IsChecked = true;
                }
                else { 
                    first.IsChecked = false;
                }
            }
        }

        private void OnOpenSelect()
        {
            this.model.selectPopup.IsOpen = true;
        }

        private bool checkBoxIsChecked;
        public bool CheckBoxIsChecked
        {
            get
            {
                return checkBoxIsChecked;
            }
            set
            {
                checkBoxIsChecked = value;
                ChangedSelecgedFlags();
                RaisePropertyChanged();
                RaisePropertyChanged("Data.EvaluateList");
            }
        }

        private void ChangedSelecgedFlags()
        {
            foreach (var e in Data.EvaluateRecordEntityList)
                e.IsSelectedFlag = CheckBoxIsChecked;
        }

        public int PageIndex { get; set; } = 1;

        private EvaluateRecordEntity selectedItem;
        public EvaluateRecordEntity SelectedItem
        {
            get
            {
                return selectedItem;
            }
            set
            {
                selectedItem = value;
                RaisePropertyChanged();
            }
        }

        #region Command
        public RelayCommand OpenSelect { get; private set; }

        public RelayCommand<ColumnListItem> ColumnCheckBox { get; private set;  }
        #endregion

        #region 数据绑定
        public EvaluationRecordEntity Data { get; private set; }

        //public ObservableCollection<ColumnListItem> InitColumnList(ObservableCollection<ColumnListItem> list) {
        //    foreach (var item in list)
        //    {
        //        char[] charArr = item.ColumnName.ToCharArray();
        //        item.ColumnName = string.Join("&#x0a;", charArr);
        //    }
        //    return list;
        //}
        #endregion
    }
}
